using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Scenario.Modeler
{
	// AnchorBarForm.cs
	// <copyright>Copyright (c) Microsoft Corporation. All rights reserved.
	// </copyright>
	// <summary>This class contains the implementation for the form that will be set
	// as the content of the anchor bar created in the AnchorBarUsage class.
	// </summary>



		/// <summary>This class contains the implementation for the form that will
		/// be set as the content of the anchor bar created in the AnchorBarUsage
		/// class.</summary>
		public class AnchorBarForm
			: System.Windows.Forms.Form 
		{

			/// <summary>Required list view variable</summary>
			private System.Windows.Forms.ListView myListView;

			/// <summary>Required by the Windows Form Designer</summary>
			private System.ComponentModel.Container components = null;

			/// <summary>The Visio Application object</summary>
			private Microsoft.Office.Interop.Visio.Application
				visioApplication = null;

			/// <summary>The Visio Application property</summary>
			/// <value>Instance of the Visio Application object</value>
			public Microsoft.Office.Interop.Visio.Application
				ParentVisioApplication 
			{

				get 
				{
					return visioApplication;
				}
				set 
				{
					visioApplication = value;
				}
			}

			/// <summary>This is the constructor for the AnchorBarForm class.
			/// </summary>
			public AnchorBarForm() 
			{
			
				// Required for Windows Form Designer support
				InitializeComponent();
			}

			/// <summary>This form overrides Dispose to clean up the component list.
			/// </summary>
			/// <param name="disposing">true to release both managed and unmanaged
			/// resources; false to release only unmanaged resources.</param>
			protected override void Dispose(bool disposing) 
			{

				if (disposing) 
				{
					if (components != null) 
					{
						components.Dispose();
					}
				}

				base.Dispose(disposing);
			}

			#region Windows Form Designer generated code
			/// <summary>Required method for Designer support - do not modify
			/// the contents of this method with the code editor.</summary>
			private void InitializeComponent() 
			{
				this.myListView = new System.Windows.Forms.ListView();
				this.SuspendLayout();
				// 
				// myListView
				// 
				this.myListView.Dock = System.Windows.Forms.DockStyle.Fill;
				this.myListView.FullRowSelect = true;
				this.myListView.MultiSelect = false;
				this.myListView.Name = "myListView";
				this.myListView.Size = new System.Drawing.Size(317, 273);
				this.myListView.TabIndex = 0;
				this.myListView.View = System.Windows.Forms.View.Details;
				this.myListView.ItemDrag +=
					new System.Windows.Forms.ItemDragEventHandler(
					this.myListView_ItemDrag);
				// 
				// AnchorBarForm
				// 
				this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
				this.ClientSize = new System.Drawing.Size(317, 273);
				this.Controls.AddRange(new System.Windows.Forms.Control[] {
																			  this.myListView});
				this.Name = "AnchorBarForm";
				this.Text = "AnchorBar Form";
				this.Load += new System.EventHandler(this.AnchorBarForm_Load);
				this.ResumeLayout(false);

			}
			#endregion

			/// <summary>This method adds the column headers and an item for each
			/// master to the list view of the form.</summary>
			/// <param name="sender">Parameter not used by this implementation
			/// </param>
			/// <param name="e">Parameter not used by this implementation</param>
			private void AnchorBarForm_Load(object sender,
				EventArgs e) 
			{

				const string column1Header = "Stencil Name";
				const string column2Header = "Master Name";

				System.Windows.Forms.ListViewItem item;
				Array stencilNames;
				Microsoft.Office.Interop.Visio.Document stencil;
				Microsoft.Office.Interop.Visio.Masters mastersInStencil;
				Microsoft.Office.Interop.Visio.Documents visioDocuments;
			
				try 
				{

					// Add column headers to the list view.
					myListView.Columns.Add(column1Header,
						100,
						System.Windows.Forms.HorizontalAlignment.Left);
					myListView.Columns.Add(column2Header,
						150,
						System.Windows.Forms.HorizontalAlignment.Left);

					// Get the stencils for this window.
					visioApplication.ActiveWindow.DockedStencils(
						out stencilNames);

					visioDocuments = visioApplication.Documents;

					// For each stencil in the template, place a reference to each
					// master shape into the ListView
					foreach (string stencilName in stencilNames) 
					{

						stencil = visioDocuments[stencilName];
						mastersInStencil = stencil.Masters;

						foreach (Microsoft.Office.Interop.Visio.Master
									 masterInStencil in mastersInStencil) 
						{
						
							// Add items and subitems to the list view.
							item = myListView.Items.Add(stencil.Name);
							item.SubItems.Add(masterInStencil.NameU);

							// Store the master ID with the item.  This will be used
							// if the user drags the item onto the Visio drawing
							// page.
							item.Tag = masterInStencil.ID;
						}
					}
				}
				catch (Exception err) 
				{
					System.Diagnostics.Debug.WriteLine(err.Message);
				}
			}
		
			/// <summary>This method handles the event when the user drags 
			/// the list view item.  The list view item can be dropped onto
			/// a Visio drawing page.  Dragging a list view item onto a Visio
			/// drawing page will result in a shape being created from the master
			/// that is being dragged.</summary>
			/// <param name="sender">Parameter not used by this implementation
			/// </param>
			/// <param name="e">Arguments of the ItemDrag event</param>
			private void myListView_ItemDrag(object sender, 
				System.Windows.Forms.ItemDragEventArgs e) 
			{

				System.Windows.Forms.ListViewItem selectedListItem;
				Microsoft.Office.Interop.Visio.Master selectedMaster;
				string stencilName;
				int masterID;
			
				try 
				{
				
					// Check if the user is dragging the list view item
					// while holding down the left mouse button.
					if (e.Button == System.Windows.Forms.MouseButtons.Left) 
					{
					
						// The list view is not configured to be multi-selectable.
						// If there is an item selected, there will be one item
						// in the SelectedItems collection.
						if (myListView.SelectedItems.Count > 0) 
						{
						
							// The stencil name is in the first column and
							// the master ID is stored in the Tag.
							selectedListItem = myListView.SelectedItems[0];
							stencilName = selectedListItem.Text;
							masterID = (int)selectedListItem.Tag;
						
							// Get a reference to the master based on what is
							// selected in the list view.
							selectedMaster = visioApplication.Documents[
								stencilName].Masters.get_ItemFromID(masterID);
						
							// Begin the drag drop operation and pass the master to
							// be dropped. Visio will automatically create a shape
							// from the selected master on the page onto which the 
							// user drops.
							myListView.DoDragDrop(selectedMaster, 
								System.Windows.Forms.DragDropEffects.Copy);
						}
					}
				}
				catch (Exception err) 
				{
					System.Diagnostics.Debug.WriteLine(err.Message);
				}
			}
		}
	}
